<template>
	<view class="padding-lr padding-bottom-big margin-bottom ft-main bg-drak">
		<use-navbar title="发票抬头"></use-navbar>
		
		<view v-for="(item, idx) in datas" :key="idx"
			class="bg-main padding-top padding-lr border-radius margin-top-sm" 
			@click="selectData(item)">
			<view class="w-full dflex-wrap-w border-line">
				<view class="fwb margin-bottom-xs desc">
					<text>{{ item.name }}</text>
				</view>
				<view class="margin-bottom-sm fs-xs">
					<text>发票抬头 · {{ item.type == 1 ? '个人抬头' : '企业抬头' }}</text>
				</view>
			</view>
			<view class="dflex-b">
				<view v-if="item.is_default == 1" class="dflex active">
					<text class="iconfont iconxuanzhongzhuangtai padding-tb-sm padding-right-sm"></text>
					<text> 默认抬头</text>
				</view>
				<view v-else class="dflex ft-dark" @tap.stop="setDefault(item)">
					<text class="iconfont iconweixuanzhongzhuangtai padding-tb-sm padding-right-sm"></text>
					<text> 设为默认</text>
				</view>
				<view v-if="source == 0 || source == 1" class="dflex">
					<view class="padding-tb-sm padding-right-sm" @tap.stop="addData('edit', item)"><text
							class="iconfont iconbianji-01 ft-dark"></text></view>
					<view class="padding-tb-sm padding-left-sm" @tap.stop="removeData(item)"><text
							class="iconfont iconlajitong-01 ft-dark"></text></view>
				</view>
			</view>
		</view>
		<view v-if="datas && datas.length" class="w-full" style="height: 160rpx;"></view>
		
		<use-empty v-if="isload && !(datas && datas.length)" e-style="round" e-type="cart" tip="暂无发票抬头" btn-tip="去添加" height="80vh" :auto="false" @goto="addData('add')"></use-empty>
		
		<view class="pos-f btn-container padding-tb bg-drak">
			<view class="dflex-b border-radius-big safe-area-inset-bottom-plus">
				<!-- <view v-if="is_mp" class="tac padding-tb-sm flex1 bg-gray" @click="importData">{{ platform_name }}导入</view> -->
				<view class="tac padding-tb-sm flex1 bg-base" @click="addData('add')">添加发票抬头</view>
			</view>
		</view>
	</view>
</template>

<script>
	const __name = 'usemall-member-invoice';
	export default {
		data() {
			return {
				env: {},

				is_mp: false,
				platform: '',
				platform_name: '',
				platform_icon: '',

				source: 0,
				isload: false,
				datas: []
			};
		},
		onLoad(option) {
			this.$api.get_env((res) => {
				this.env = res;
				// console.log(this.env);
				this.is_mp = this.env.is_mp;
				this.platform = this.env.platform;
				this.platform_icon = this.env.platform_icon;
				this.platform_name = this.env.platform_name;
			});

			this.source = option.source || 0;
		},
		onShow() {
			this.loadData();
		},
		methods: {
			// 加载数据
			loadData() {
				if (!this.isload) {
					uni.showLoading({
						title: '加载中'
					});
				}
				this.$db[__name].where('create_uid == $cloudEnv_uid').tolist({
					orderby: 'is_default desc'
				}).then(res => {
					this.isload = 1;
					if (res.code === 200) {
						this.datas = res.datas;
						return;
					}
					this.$api.msg(res.msg);
				}).finally(res => {
					uni.hideLoading();
				});
			},
			// 导入发票抬头
			importData() {
				let _this = this;

				uni.chooseInvoiceTitle({
					success(res) {
						let addr = {};

						addr.is_default = 0;
						addr.consignee = res.userName;
						addr.mobile = res.telNumber;
						addr.addr_detail = res.detailInfo;
						addr.province_name = res.provinceName;
						addr.city_name = res.cityName;
						addr.area_name = res.countyName || '';

						// #ifdef MP-ALIPAY
						addr.area_name = res.result.area;
						// #endif

						addr.address = `${addr.province_name}-${addr.city_name}-${addr.area_name}`;
						addr.addr_source = _this.$env.platform;

						if (!addr.mobile) {
							_this.$api.msg('手机不存在')
							return;
						}
						console.log('uni chooseInvoiceTitle', addr)
						_this.$db[__name].add(addr).then(res => {
							if (res.code === 200) {
								_this.$api.msg('导入成功');
								_this.loadData();
								return;
							}

							_this.$api.msg(res.msg);
						});

					},
					fail(err) {
						if (err.errMsg.indexOf('cancel') !== -1) {
							_this.$api.msg('已取消')
						} else {
							uni.showModal({
								content: '打开授权',
								success: e => {
									if (e.confirm) {
										uni.openSetting({

										})
									}
								}
							});
						}
					}
				})
			},
			// 默认地址
			setDefault(res) {
				uni.showModal({
					content: '设为默认',
					success: async (res) => {
						if (res.confirm) {
							// 把默认为1是的改成 0否
							await this.$db[__name]
								.where('create_uid == $env.uid && is_default == 1')
								.update({
									is_default: 0
								})
								.then(res => {

								});

							// 把当前 _id 改成 1是
							await this.$db[__name]
								.update(res._id, {
									is_default: 1
								})
								.then(res => {
									if (res.code === 200) {
										this.loadData();
									}
								});
						} 
					}
				});
			},
			// 选择发票抬头
			selectData(res) {
				if (this.source == 1) {
					uni.$emit('__event_choice_invoice', res);
					uni.navigateBack();
				}
			},
			// 添加|编辑
			addData(type, res) {
				res = res || {
					id: 0
				};
				uni.navigateTo({
					url: `/sub-user/pages/invoice/invoice-edit?type=${type}&id=${res._id}`
				});
			},
			// 删除
			removeData(res) {
				uni.showModal({
					content: '删除',
					success: (res) => {
						if (res.confirm) {
							this.$db[__name].remove(res._id).then(res => {
									if (res.code === 200) {
										this.loadData();
									}
								});
						}
					}
				});

			}
		}
	};
</script>

<style lang="scss">
	page {
		background: $page-color-base;
	}

	.desc {
		font-size: $font-lg;
	}

	.btn-container {
		left: 20rpx;
		right: 20rpx;
		bottom: 0rpx;
	}
</style>
